package org.myself.meeting.controller;

import org.myself.meeting.model.*;
import org.myself.meeting.service.DepartmentService;
import org.myself.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class DepartmentController {

    @Autowired
    DepartmentService ds;
    @Autowired
    EmployeeService employeeService;

    //部门管理
    @RequestMapping("/departments")
    @ResponseBody
    public Result departments(@RequestBody Base base){
        Map<String,List<Department>> map = new HashMap<String, List<Department>>();
        map.put("deps",ds.getAllDeps());
        return new Result(200,"查询成功",map);
    }
    //添加部门
    @RequestMapping("/adddepartment")
    @ResponseBody
    public Result addDepartment(@RequestBody AdddepartmentAcc acc){
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        Integer value = ds.addDepartment(acc.getDepartmentname(),emp);
        if (value == 1){
            return new Result(200,"success",null);
        }
        return new Result(400,"fail",null);
    }
//    //删除部门
//    @RequestMapping("/department/del")
//    @ResponseBody
//    public Result deleteDepartment(@RequestBody DeletedepartmentAcc acc){
//        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
//        ds.deleteDepartment(acc.getDepartmentid(),acc.getDepartmentname(),emp);
//        return new Result(200,"success",null);
//    }
    //返回字符串  更新部门
    @RequestMapping("/updatedep")
    @ResponseBody
    public Result updateDepartment(@RequestBody UpdateDepAcc acc){
        Employee emp =employeeService.getEmpsByUsername(acc.getUsername()) ;
        Integer value = ds.updateDepartment(acc.getId(),acc.getName(),emp);
        if (value == 1){
            return new Result(200,"success",null);
        }
        return new Result(400,"fail",null);
    }

}
